package com.medical.goods.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class MyMqConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 延时队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
         /*
           Queue(String name,  队列名字
           boolean durable,  是否持久化
           boolean exclusive,  是否排他
           boolean autoDelete, 是否自动删除
           Map<String, Object> arguments) 属性
        */
        HashMap<String, Object> arguments = new HashMap<>();

        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000); // 消息过期时间 1分钟  设置为30分钟
        return new Queue("order.dalay.queue",true,false,false,arguments);
    }

    /**
     * 普通订单队列
     * @return
     */
    @Bean
    public Queue OrderQueue(){
        return new Queue("order.release.order.queue",true,false,false);
    }
    /**
     * 普通队列交换机
     */

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange("order-event-exchange",true,false);
    }
    /**
     * 绑定延时队列
     * @return
     */
    @Bean
    public Binding orderCreateBinding(){
        return new Binding("order.dalay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",null);
    }
    /**
     * 绑定普通队列
     */
    @Bean
    public Binding orderReleaseBinding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }
    /**
     * 秒杀队列
     */
    @Bean
    public Queue SeckillQueue(){
        return new Queue("seckill.order.queue",true,false,false);
    }

    /**
     * 秒杀交换机
     * @return
     */
    @Bean
    public Exchange SecillEventExchange(){
        return new TopicExchange("seckill-event-exchange",true,false);
    }


    /**
     * 绑定秒杀队列
     * @return
     */
    @Bean
    public Binding seckillReleaseBinding(){
        return new Binding("seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "seckill-event-exchange",
                "seckill.release.order",null);
    }

}
